﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;

public class HomeUI : MonoBehaviour
{
    private UserModel user;
    private StageModel stage;

    private Text TextName { get; set; }
    private Text TextLevel { get; set; }
    private Text TextExp { get; set; }
    private Slider SliderExp { get; set; }
    private Text TextGold { get; set; }
    private Text TextCrystal { get; set; }
    private GameObject StartLock { get; set; }
    private GameObject SoundLock { get; set; }
    private GameObject FlyLock { get; set; }
    private GameObject DreamLock { get; set; }

    void Awake()
    {
        
        user = Engine.GameSystem.ReadUser();
        stage = Engine.GameSystem.ReadStage();
    }

    void Start()
    {
        TextName = transform.FindChild("User/TextName").GetComponent<Text>();
        TextLevel = transform.FindChild("User/TextLevel").GetComponent<Text>();
        TextExp = transform.FindChild("User/TextExp").GetComponent<Text>();
        SliderExp = transform.FindChild("User/SliderExp").GetComponent<Slider>();
        TextGold = transform.FindChild("Assets/ImageGold/TextValue").GetComponent<Text>();
        TextCrystal = transform.FindChild("Assets/ImageCrystal/TextValue").GetComponent<Text>();
        StartLock = transform.FindChild("Level/ButtonStart/ImageLock").gameObject;
        SoundLock = transform.FindChild("Level/ButtonSound/ImageLock").gameObject;
        FlyLock = transform.FindChild("Level/ButtonFly/ImageLock").gameObject;
        DreamLock = transform.FindChild("Level/ButtonDream/ImageLock").gameObject;

        TextName.text = user.userName;
        TextLevel.text = user.level.ToString();
        int targetExp = 100 + (int)Math.Pow(user.level, 2) * 120;
        TextExp.text = user.exp.ToString() + " / " + targetExp.ToString();
        SliderExp.value = (float)user.exp / (float)targetExp;
        TextGold.text = user.gold.ToString();
        TextCrystal.text = user.crystal.ToString();

        SetStageLock(StartLock, 1);
        SetStageLock(SoundLock, 2);
        SetStageLock(FlyLock, 3);
        SetStageLock(DreamLock, 4);
    }

    private void SetStageLock(GameObject origin, int level)
    {
        bool active = false;
        switch (level)
        {
            case 1:
                active = stage.stage_01 == 0 ? true : false;
                break;
            case 2:
                active = stage.stage_02 == 0 ? true : false;
                break;
            case 3:
                active = stage.stage_03 == 0 ? true : false;
                break;
            case 4:
                active = stage.stage_04 == 0 ? true : false;
                break;
        }

        origin.SetActive(active);
        origin.transform.parent.GetComponent<Button>().interactable = !active;
    }

    public void ButtonStart()
    {
        Engine.UISystem.Next("Loading", "Start");
    }

    public void ButtonSound()
    {
        Engine.UISystem.Next("Loading", "Sound");
    }

    public void ButtonFly()
    {
        Engine.UISystem.Next("Loading", "Fly");
    }

    public void ButtonDream()
    {
        Engine.UISystem.Next("Loading", "Dream");
    }
}
